/**
 * 
 */
package com.example.radioclub.utilities;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources.Theme;
import android.database.DataSetObserver;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.radioclub.R;
import com.example.radioclub.data.Constants;
import com.example.radioclub.types.Genre;

/**
 * This class defines how you would like to choose category from many category.
 * 
 * @author wizard
 * 
 */
public class GenreChooser {
	private final String TAG = Constants.APP_TAG + "Genre Chooser";
	private String parentId = null, childId = null;
	private Context context;
	private ArrayList<Genre> genres = null;
	private AlertDialog.Builder builder = null;
	private AlertDialog alertDialog = null;
	private TextView place = null;

	public GenreChooser(Context context, String genreJSON, TextView place) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.genres = FunctionUtils.getGenreFromJSON(genreJSON);
		this.place = place;
	}

	public ArrayList<Genre> getGenres() {
		return genres;
	}

	public String getParentId() {
		return parentId;
	}

	public String getChildId() {
		return childId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public void setChildId(String childId) {
		this.childId = childId;
	}

	public void putParentId(TextView place) {
		place.setText(getParentId());
	}

	public void showGenreChooserDialog() {
		View view = LayoutInflater.from(this.context).inflate(
				R.layout.genre_list_layout, null);
		ExpandableListView genreList = (ExpandableListView) view
				.findViewById(R.id.genre_list);
		genreList.setAdapter(new ExpandableListAdapter() {

			@Override
			public void unregisterDataSetObserver(DataSetObserver observer) {
				// TODO Auto-generated method stub

			}

			@Override
			public void registerDataSetObserver(DataSetObserver observer) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onGroupExpanded(int groupPosition) {
				// TODO Auto-generated method stub
				Log.i(TAG, getGenres().get(groupPosition).getGenreId());
				setParentId(getGenres().get(groupPosition).getGenreId());
			}

			@Override
			public void onGroupCollapsed(int groupPosition) {
				// TODO Auto-generated method stub
				setParentId(null);
				setChildId(null);
			}

			@Override
			public boolean isEmpty() {
				// TODO Auto-generated method stub
				return (getGenres() == null) ? true : false;
			}

			@Override
			public boolean isChildSelectable(int groupPosition,
					int childPosition) {
				// TODO Auto-generated method stub
				return true;
			}

			@Override
			public boolean hasStableIds() {
				// TODO Auto-generated method stub
				return true;
			}

			@Override
			public View getGroupView(int groupPosition, boolean isExpanded,
					View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub
				final int groupPos = groupPosition;
				View view = LayoutInflater.from(context).inflate(
						R.layout.genre_layout, null);
				LinearLayout layout = (LinearLayout) view
						.findViewById(R.id.layout);
				TextView genreName = (TextView) view
						.findViewById(R.id.genre_name);
				genreName
						.setText(getGenres().get(groupPosition).getGenreName());
				layout.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						setParentId(getGenres().get(groupPos).getGenreName());
						Log.i(TAG + " Dialog out by OnClickListener",
								getParentId());
						putParentId(place);
						alertDialog.dismiss();
					}
				});
				return view;
			}

			@Override
			public long getGroupId(int groupPosition) {
				// TODO Auto-generated method stub
				return groupPosition;
			}

			@Override
			public int getGroupCount() {
				// TODO Auto-generated method stub
				return getGenres().size();
			}

			@Override
			public Object getGroup(int groupPosition) {
				// TODO Auto-generated method stub
				return getGenres().get(groupPosition);
			}

			@Override
			public long getCombinedGroupId(long groupId) {
				// TODO Auto-generated method stub
				return 0;
			}

			@Override
			public long getCombinedChildId(long groupId, long childId) {
				// TODO Auto-generated method stub
				return 0;
			}

			@Override
			public int getChildrenCount(int groupPosition) {
				// TODO Auto-generated method stub
				return getGenres().get(groupPosition).getGenres().size();
			}

			@Override
			public View getChildView(int groupPosition, int childPosition,
					boolean isLastChild, View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub
				View view = LayoutInflater.from(context).inflate(
						R.layout.genre_layout, null);
				TextView genreName = (TextView) view
						.findViewById(R.id.genre_name);
				genreName.setText(getGenres().get(groupPosition).getGenres()
						.get(childPosition).getGenreName());
				return view;
			}

			@Override
			public long getChildId(int groupPosition, int childPosition) {
				// TODO Auto-generated method stub
				return childPosition;
			}

			@Override
			public Object getChild(int groupPosition, int childPosition) {
				// TODO Auto-generated method stub
				return childPosition;
			}

			@Override
			public boolean areAllItemsEnabled() {
				// TODO Auto-generated method stub
				return true;
			}
		});

		this.builder = new AlertDialog.Builder(this.context);
		this.builder.setTitle("Choose genre").setCancelable(true);
		this.builder.setView(view).setPositiveButton("OK",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						Log.i(TAG + " Dialog out!", getParentId());
						dialog.dismiss();
					}
				});
		this.alertDialog = this.builder.create();
		this.alertDialog.show();
	}
}
